{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Import relevant libraries\n",
    "import pandas as pd\n",
    "import matplotlib\n",
    "matplotlib.use('Qt5Agg')  #If specifying, it needs to be done before pyplot, otherwise itll throw warnings, ignore if running twice\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "#I find this package to be very user friendly for those who are new/don't like coding\n",
    "#Though, please note, with this package (as opposed to tknter) you will need to use \n",
    "#the draw/show functions to update plots if out of line\n",
    "\n",
    "\n",
    "#Create plots inline rather than opening up the GUI\n",
    "#Note: The GUI can be used to inspect plots further or change appearances without editing code\n",
    "# % matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = range(0,10) #Creates a list 0,1,...10\n",
    "y = [number**2 for number in x] #Creates a squared Value\n",
    "y2 = x #Creates a linear value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Create two sublots. The first element being how many rows, the second being how many columns.\n",
    "#e.g (2,2) would be four plots total, a quad.\n",
    "#This also creates two elements, fig which is your overall figure and ax, which is indexed 0 & 1 representing the \n",
    "#axes of the individual plots\n",
    "plt.clf()\n",
    "plt.close('all')\n",
    "fig, ax = plt.subplots(2,1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Skipping straight to how to plot\n",
    "#Similiar to MATLAB you only need x and y, but you can specify things such as linewidth, style, alpha etc\n",
    "# You can also tag these as items L1,L2\n",
    "l1 = ax[0].scatter(x,y,color = 'xkcd:green', s=200) #See XKCD colors for more at https://xkcd.com/color/rgb/\n",
    "l2 = ax[1].plot(x,y2,color = (0.1, 0.2, 0.5),linewidth = 5) #Also takes RGB\n",
    "# ax[1].get_lines()[0] #This is how to obtain a line element later if you forgot\n",
    "#If you mess up, remember plt.cla(), plt.clf() and ax[i].clear() \n",
    "# all work to clear the current axes, current figure, and specified axis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Specifying grid lines per axis\n",
    "# ax[0].grid('on','minor',linestyle='-.',linewidth=.5, color = 'xkcd:black') \n",
    "ax[0].grid('on','major',linestyle='-.',linewidth=.5, color = 'xkcd:black') \n",
    "\n",
    "ax[1].grid('on','major',linestyle='--',linewidth=.5,alpha = 10) #Alpha is comparable to transparency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.axis.XTick at 0x28ee93d2358>,\n",
       " <matplotlib.axis.XTick at 0x28ee93a0ef0>,\n",
       " <matplotlib.axis.XTick at 0x28ee93b8fd0>,\n",
       " <matplotlib.axis.XTick at 0x28eecf26908>,\n",
       " <matplotlib.axis.XTick at 0x28eecf26f60>,\n",
       " <matplotlib.axis.XTick at 0x28eecf2e5f8>,\n",
       " <matplotlib.axis.XTick at 0x28eecf2ec88>,\n",
       " <matplotlib.axis.XTick at 0x28eecf37320>,\n",
       " <matplotlib.axis.XTick at 0x28eecf379e8>,\n",
       " <matplotlib.axis.XTick at 0x28eecf3d0f0>]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Change tick frequency\n",
    "ax[0].tick_params(axis='x', which='minor', bottom=False)\n",
    "ax[0].set_xticks(np.arange(min(x),max(x)+1,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib.ticker import (MultipleLocator, FormatStrFormatter,\n",
    "                               AutoMinorLocator)\n",
    "ax[0].xaxis.grid(True, which='minor')\n",
    "ax[1].xaxis.grid(True,which='minor')\n",
    "ax[0].xaxis.set_major_locator(MultipleLocator(1))\n",
    "ax[0].yaxis.set_major_locator(MultipleLocator(4))\n",
    "ax[0].xaxis.set_major_formatter(FormatStrFormatter('%d'))\n",
    "\n",
    "# For the minor ticks, use no labels; default NullFormatter.\n",
    "ax[0].xaxis.set_minor_locator(MultipleLocator(.25))\n",
    "\n",
    "# fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 10)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ax[0].set_xlim([0,4])\n",
    "ax[0].set_ylim([0,16])\n",
    "ax[1].set_ylim([0,10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.figure.Figure at 0x28ee91e90b8>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ax[0].set_ylabel(\"ax[0].set_ylabel()\",fontsize=20)\n",
    "ax[1].set_ylabel(\"ax[1].set_ylabel()\",fontsize=20)\n",
    "ax[0].set_xlabel(\"ax[0].set_xlabel()\",fontsize=20)\n",
    "ax[1].set_xlabel(\"ax[1].set_xlabel()\",fontsize=20)\n",
    "#Note if you do not run tight_layout(), ax[0] x label will not appear\n",
    "# fig.tight_layout()\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax2 = ax[1].twinx()  # instantiate a second axes that shares the same x-axis\n",
    "ax2.set_ylabel('ax2.set_ylabel()', color='xkcd:red',fontsize = 20)  # we already handled the x-label with ax1\n",
    "ax2.plot(x, y, color='xkcd:red',linewidth = 5)\n",
    "ax2.tick_params(axis='y', labelcolor='xkcd:red')\n",
    "# fig.tight_layout()  # otherwise the right y-label is slightly clipped\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.figure.Figure at 0x28ee91e90b8>"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ax[0].set_title(\"ax[0].set_title\",fontsize= 24)\n",
    "ax[1].set_title(\"ax[1].set_title\",fontsize = 24)\n",
    "# ax2.set_title(\"Subtitle for Axis 1 Secondary\",fontsize = 24)\n",
    "fig.suptitle(\"How To Plot in Matplotlib\",y=.98,x = .5, fontsize = 30)\n",
    "# fig.tight_layout()\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.figure.Figure at 0x28ee91e90b8>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#align second y axis\n",
    "ax2.set_ylim([0,100])\n",
    "ax[1].set_yticks(np.linspace(0, ax[1].get_ybound()[1], 5, endpoint = True))\n",
    "ax2.set_yticks(np.linspace(0,100,5, endpoint = True))\n",
    "fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "leg1 = ax[1].legend([\"ax[1].legend()\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x28eed282e48>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.legend(['Entire','Figure','Legend'],ncol = 3,loc = 'upper center',bbox_to_anchor = [.5,.95],fontsize=16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "props = dict(boxstyle='round', facecolor='silver', alpha=0.23)\n",
    "annon = plt.text(0.2, 0.475, \"Created by: \\n M.Hatzipetro\\n 7/11/19\",ha = 'center', fontsize=14, transform=fig.transFigure,bbox=props)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax[1].minorticks_on()\n",
    "ax[1].tick_params(axis='x', which='minor', direction='out',width = 2)\n",
    "ax[1].xaxis.grid(False, which='minor')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# fig.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax[0].tick_params(axis='both', which='major', labelsize=14)\n",
    "ax[1].tick_params(axis='both', which='major', labelsize=14)\n",
    "ax2.tick_params(axis='both', which='major', labelsize=14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.draw()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\mh\\\\Anaconda3\\\\lib\\\\site-packages\\\\matplotlib\\\\mpl-data\\\\matplotlibrc'"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import matplotlib\n",
    "matplotlib.matplotlib_fname()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
